package com.atguigu.gulimall.order.pay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 支付宝异步通知、用的最大努力通知方案
 */

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2016092300575710";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDMA66v2oqhsf/495EObsxMD2eYPp2m9Xkbz+AfESjzp95iXcTlguJ5mBuZlxs44s+e8yO/ujYn7Jo0KRq3T0RKfx7A7AvUVvk51SFKaijXHxIA2m1QfcT39ZRc3eIce8Tr0FMbgDN+/5628a/2BvYt8BegntukbM1glXwUSeIlhfZS4rOI0gEor5Ec8nQn8KhCt1K1jCKnWVJw9zWMacbvJBX0G+QdzkEosI5CTPm7FDWCgVvh/4eJowXvxrdMofvSk5ULnoYObCIFNBknGRyDgWOsIVxzleRzMoc+Bx5msL4+nRNqwOc7uPPDs9XAQlW7tmJLAksQmdbHDqKrWA6ZAgMBAAECggEAToZIUYVzgKMrnB9l5ME+vzPast2+/olTMSJE6JBJS+dcEE55P8Gt/BFQ5kAkCTQ7p7l8FArPV3SZMeR0VQQci/P72VPBhdxoFxtysdrEl3BtFIj+NFIEY1BR56C7rCzzcbSAmIAy0fn/k/jYuoCZdrsHzRn3/oSmVDuVQ5XghoIllYidp92CfA9RayOa/N7D1vxxy2VaSOCDNLUb0snUB34n6Tq05QZ3hHWU3MMO6+OtlD2sEAxr/Vc712uiiD3OWp26NKQWxco52lbV4g30x7KlCX/fSHKVRpjER/7IPJmX6CKfd3++SY3byrFaGVl6PyhTChpZKFq3WsGahBPl1QKBgQD9yh6le6l/OkORqLJhAmaiNR97gvQuj8t5BLhke9mrKTA6DcRL4rBwQ2a31pU9JYZZQ/W8PX9M3s4FzsboQkY1oUH9dLJaXhhLNbZ1YRo2ZI1gWuZAGDkq5lkyJFfr0Y+AtIVIIAhmYecSkLbdQYTYmMR7npAWgmcjeWkEsaTXBwKBgQDNypPwKzofQAd9pehO0OGroYwumXqthqno1h+IeOzWsr1BTSG20lu6PASUcNqPV4zFCP+epp324GzDAcwkc+58w0akv6Vo9R158cviOCESNdBVPGBWmMiEPMklhALhj9gYBceK4yGVsXUGrC6ZiX3ujS97LLYL5I3mNEaVDtXlXwKBgBPqg6Mq5C9GzDynB3CTdqhuEM1phm4M/MO/ehTwnzFOZyl3IRtva7czouD0ZvvIcRWaPrOKA+5tYa+U/YkGgmn9AKe6O7DZx4BHc80cVKlGFM4l9AG9pObX8tH7hibMaD2KbRcu5Fncfvy2/JK6bIlu26Rz2gzaKOvgwDwNEMmLAoGAXMbZj0tNtsz9E456fx34v5HjSxbykczGSSxPpw0taVXHsOr/KhcIN5vbTLjcMAd+dU4rP/FKAgUHw5rJ2B1UBXdcH+YuwnTPRyzViDVZ44XTWLoRjLjwnX1yCbHMQrjA6YOhhGIFwCDy64EwuROdbLDGGTf9EruFpaUntQetMu0CgYEAg0wIVm3+3uIkmHfDqLqt5ePtGIDnOogvpw84qBe4iq2CCrUZtBYVj9GyeTcuvH+GcRkSX3b5zSaQJs3jAeAPdCo4k5k8DLRTr15rPMLX5Pgqrrp93i4fpeV0qg7RAz2IcXUeAn5DO2yGemq4yXBH3bgVeTHUEcvRSf8ZLLVA4vY=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2pBRHkb6ZTfe/YcRdTbkaJNu95THc5R0OaNCYYBnBX15GxSYuswMSAlwOP1Ew3xRlH5c51ymS6GKoOfdsrqotQ54w5Exy5tg0KR/wJmwbSu+dqMQ7GvfvxwT9rcEDHzx+HxpY2zRQ6mHN/pVJoJu7OPmYBeYqIAL8iyuDjD8lsqX60gkQTugZlFVGqqtCkauE+tgs0h6Z8JNOM2x/AjthKbrtTYBSAb1vQpgHOBpGk3lcrTDWv/Nd5POVmRajV+a7YrnjCPQjdV8G/NXdo3b3pS1cevjeOJ7XekAoGElHuvhAWCzY4gRx2MRaEGlFwvUgatHFqXxXKn+F3DuznIv0wIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://uexpo.natapp1.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";
private String timeOut = "30m";
    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient (gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest ();
        alipayRequest.setReturnUrl (return_url);
        alipayRequest.setNotifyUrl (notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no ();
        //付款金额，必填
        String total_amount = vo.getTotal_amount ();
        //订单名称，必填
        String subject = vo.getSubject ();
        //商品描述，可空
        String body = vo.getBody ();

        alipayRequest.setBizContent ("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"" + timeOut + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute (alipayRequest).getBody ();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println ("支付宝的响应：" + result);

        return result;

    }
}
